using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using CMS.BIZ;
using System.Web.Security;

namespace CMS.Web.UI.UserControls
{
    public class MembershipUserSelfEditor : UserControl
    {

        protected Label lUserName;
        protected TextBox tbLastName;
        protected TextBox tbFirstName;
        protected TextBox tbEmail;
        protected DropDownList ddlCulture;
        protected TextBox tbImagePath;

        protected Button bValidate;

        public MembershipUserSelfEditor()
        {
            base.Init += new EventHandler(MembershipUserSelfEditor_Init);
        }

        void MembershipUserSelfEditor_Init(object sender, EventArgs e)
        {
            if (bValidate != null) bValidate.Click += new EventHandler(Validate_Click);
            else
            {
                if (tbEmail != null) tbEmail.ReadOnly = true;
                if (tbFirstName != null) tbFirstName.ReadOnly = true;
                if (tbFirstName != null) tbLastName.ReadOnly = true;
                if (tbImagePath != null) tbImagePath.ReadOnly = true;
                if (ddlCulture != null) ddlCulture.Enabled = false;
            }
            if (!Page.IsPostBack)
            {
                BindCulture();
                BizMembershipUser user = Page.BizMembershipUser;
                lUserName.Text = user.UserName;
                if(tbEmail != null) tbEmail.Text = user.Email;
                if(tbFirstName != null) tbFirstName.Text = user.FirstName;
                if (tbFirstName != null) tbLastName.Text = user.LastName;
                if (tbImagePath != null) tbImagePath.Text = user.ImagePath;
                if (ddlCulture != null && !string.IsNullOrEmpty(user.CultureCode)) ddlCulture.SelectedValue = user.CultureCode;
            }
        }

        private void BindCulture()
        {
            List<BizCulture> list = Page.BizCultureProvider.ReadAll();
            ddlCulture.DataSource = list;
            ddlCulture.DataValueField = "CultureCode";
            ddlCulture.DataTextField = "CultureName";
            ddlCulture.DataBind();
            ddlCulture.SelectedValue = Page.CultureCode;
        }

        void Validate_Click(object sender, EventArgs e)
        {
            BizMembershipUser user = Page.BizMembershipUser;
            if (user != null)
            {
                if (tbFirstName != null) user.LastName = tbLastName.Text;
                if (tbFirstName != null) user.FirstName = tbFirstName.Text;
                if (tbEmail != null) user.Email = tbEmail.Text;
                if (tbImagePath != null) user.ImagePath = tbImagePath.Text;
                if (ddlCulture != null) user.CultureCode = ddlCulture.SelectedValue;
                Membership.UpdateUser(user);
            }
        }

    }
}
